package com.api.shopapi.mapper;

import com.api.shopapi.entity.Elderly;
import com.api.shopapi.entity.Mediciner;
import com.api.shopapi.entity.dto.MedicinerDTO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface MedicinerMapper {

    //查询全部医生
    @Select("select n.*,u.Name,u.Phone,u.Status from mediciner n,user u where u.InstitutionCode = #{institutionCode} " +
            "and u.Status=0 " +
            "and n.UserCode = u.UserCode ")
    List<MedicinerDTO> getList(@Param("institutionCode") String institutionCode);

    @Select("<script>" +
            "select n.*,u.Name,u.Phone,u.DepartmentCode,u.DepartmentName,u.Status from mediciner n,user u where u.InstitutionCode = #{institutionCode} " +
            "and n.UserCode = u.UserCode " +

            "<if test ='name !=null and name!=\"\" '>" +
            "and u.Name like concat('%',#{name},'%') " +
            "</if>" +
            "<if test ='status !=-1 '>" +
            "and u.Status =#{status} " +
            "</if>" +

            " order by u.CreateTime desc"+
            "</script>")
    List<MedicinerDTO> getPageList(@Param("institutionCode") String institutionCode,
                                   @Param("name") String name,
                                   @Param("status") int status);

    @Insert("insert into mediciner " +
            "(MedicinerCode," +
            "UserCode," +
            "IdCard," +
            "CreateTime," +
            "Address,"+
            "InstitutionCode) " +
            "values (#{mediciner.MedicinerCode} ," +
            "#{mediciner.UserCode} ," +
            "#{mediciner.IdCard} ," +
            "#{mediciner.CreateTime} ," +
            "#{mediciner.Address} ," +
            "#{mediciner.InstitutionCode} )")
    int insert(@Param("mediciner") Mediciner mediciner);


    @Insert("<script>" +
            "insert into mediciner (MedicinerCode,UserCode,IdCard,CreateTime,Address,InstitutionCode) values " +

            "<foreach collection='list' item='obj' separator=','>" +
            "(#{obj.MedicinerCode},#{obj.UserCode},#{obj.IdCard}," +
            "#{obj.CreateTime},#{obj.Address},#{obj.InstitutionCode})" +
            "</foreach>" +

            "</script>")
    Integer batchInsert(List<Mediciner> list);

    @Update("update mediciner" +
            " set" +
            " userCode=#{mediciner.UserCode}," +
            " Address=#{mediciner.Address}," +
            " IdCard=#{mediciner.IdCard}" +
            " where MedicinerCode=#{mediciner.MedicinerCode} ")
    int updateByMedicinerCode(@Param("mediciner") Mediciner mediciner);

    @Delete("delete from mediciner where MedicinerCode=#{medicinerCode} ")
    int deleteByMedicinerCode(@Param("medicinerCode") String medicinerCode);

    @Select("select  * from  mediciner where MedicinerCode=#{medicinerCode} limit 1 ")
    Mediciner getByMedicinerCode(@Param("medicinerCode") String medicinerCode);

    @Select("select  * from  mediciner where UserCode=#{userCode} limit 1 ")
    Mediciner getByUserCode(@Param("userCode") String userCode);


    @Select("select  * from  mediciner where idCard=#{idCard} limit 1 ")
    Mediciner getByIdCard(@Param("idCard") String idCard);


    @Select("<script>" +
            "select * from mediciner " +
            " where" +
            " InstitutionCode in (select institutionCode from institution where governmentCode=#{governmentCode} )" +
            " order by createTime desc"+
            "</script>")
    List<MedicinerDTO> listByGovCode(@Param("governmentCode") String governmentCode);

    @Select("<script>" +
            "select medicinerCode from mediciner " +
            " where 1=1" +

            "<if test ='institutionCode == \"\" '>" +
            " and InstitutionCode in (select institutionCode from institution where governmentCode=#{governmentCode} )" +
            "</if>" +
            "<if test ='institutionCode != \"\" '>" +
            " and institutionCode = #{institutionCode} " +
            "</if>" +
            " order by createTime desc"+
            "</script>")
    List<Mediciner> listByGovCodeInsCode(@Param("governmentCode") String governmentCode,
                                         @Param("institutionCode") String institutionCode);


    @Select("<script>" +
            "select medicinerCode from mediciner " +
            " where " +
            " institutionCode = #{institutionCode} " +
            "</script>")
    List<Mediciner> listByInsCode(@Param("institutionCode") String institutionCode);



    @Select("<script>" +
            "select IdCard from mediciner where IdCard in" +
            "<foreach collection='list' item='item' open='(' separator=',' close=')' >" +
            "#{item.IdCard}"+
            "</foreach>" +
            "</script>")
    List<String> batchSelectIdCard(List<Mediciner> list);


    @Select("select mediciner_staff_rel.StaffCode from mediciner left join mediciner_staff_rel on mediciner_staff_rel.MedicinerCode = mediciner.MedicinerCode where mediciner.UserCode = #{userCode}")
    String getStaffCodeByUserCode(@Param("userCode") String userCode);


    @Update("update mediciner" +
            " set" +
            " InstitutionCode=#{mediciner.InstitutionCode}," +
            " IdCard=#{mediciner.IdCard}" +
            " where MedicinerCode=#{mediciner.MedicinerCode} ")
    void updateIdCardAndInstitution(Mediciner mediciner);
}
